package org.jeecgframework.web.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.jeecgframework.core.util.DateUtils;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.web.service.ZyIpReportXXServiceI;
import org.jeecgframework.web.system.pojo.base.TSUser;
import org.jeecgframework.web.system.service.UserService;
import org.jeecgframework.web.util.StaticValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;



@Scope("prototype")
@Controller
@RequestMapping("/zyIpReportXXController")
public class ZyIpReportXXController {

	@Autowired
	private UserService userService;
	
	@Autowired
	private ZyIpReportXXServiceI zyIpReportXXServiceI;
	
	/**
	 * 巡检进度报告 - 页面跳转
	 */
	@RequestMapping(params="ipReportA")
	public ModelAndView ipReportA(HttpServletRequest request){
		TSUser user = ResourceUtil.getSessionUserName();
		Integer userRoleIndex = 0;
		if(userService.hasRole(user.getUserName(),StaticValue.CHAIN_ROLE_LEADER)){//院线用户
			userRoleIndex = 1;
		}
		request.setAttribute("userRoleIndex", userRoleIndex);
		return new ModelAndView("com/zybk/chain_report/ipReportA");
	}
	
	/**
	 * 查询 巡检进度报告 报表 数据
	 * @param chain 院线
	 * @param typeId 机型
	 * @param territoryL2 省
	 * @param territoryL3 市
	 */
	@ResponseBody
	@RequestMapping(params="queryIpReportAData")
	public JSONObject queryIpReportAData(
			@RequestParam(value="year",required=false)Integer year,
			@RequestParam(value="chain",defaultValue="")String chain,
			@RequestParam(value="typeId",defaultValue="")String typeId,
			@RequestParam(value="territoryL2",defaultValue="")String territoryL2,
			@RequestParam(value="territoryL3",defaultValue="")String territoryL3){
		JSONObject json = new JSONObject();
		
		if(year != null && !year.equals("2016")){//如果是本年度
			
			Map<String, Object> IpProgress2017Data = zyIpReportXXServiceI.query2017IpProgress(chain, typeId, territoryL2, territoryL3, year);
			json.putAll(IpProgress2017Data);
			
			//累计进度表 数据查询（线饼图）
			JSONObject linePieJson = zyIpReportXXServiceI.queryLinePieChartData(chain, typeId, territoryL2, territoryL3, year);
			json.accumulate("linePieJson", linePieJson);//累计进度表
		}else{
			
			Map<String, Object> IpProgress2016Data = zyIpReportXXServiceI.query2016IpProgress(chain, typeId, territoryL2, territoryL3);
			json.putAll(IpProgress2016Data);
		}
		
		return json;
	}
	
	/**
	 * 巡检异常报告 - 页面跳转
	 */
	@RequestMapping(params="ipReportB")
	public ModelAndView ipReportB(HttpServletRequest request){
		TSUser user = ResourceUtil.getSessionUserName();
		Integer userRoleIndex = 0;
		if(userService.hasRole(user.getUserName(),StaticValue.CHAIN_ROLE_LEADER)){//院线用户
			userRoleIndex = 1;
		}
		request.setAttribute("userRoleIndex", userRoleIndex);
		return new ModelAndView("com/zybk/chain_report/ipReportB");
	}
	
	/**
	 * 巡检异常报告 - 饼图数据
	 */
	@ResponseBody
	@RequestMapping(params="queryIpReportBPieChartData")
	public Map<String, Object> queryIpReportBPieChartData(
			@RequestParam(value="year",required=false)Integer year,
			@RequestParam(value="chain",defaultValue="")String chain,
			@RequestParam(value="typeId",defaultValue="")String typeId,
			@RequestParam(value="irExType",defaultValue="")String irExType){
		return zyIpReportXXServiceI.queryIpReportBPieChartData(irExType, chain, typeId, year);
	}
	
	/**
	 * 院线展板
	 */
	@RequestMapping(params="chainBan")
	public ModelAndView chainBan(HttpServletRequest request){
		return new ModelAndView("main/chainHome");
	}
	
}
